
/*
 * PanelContrasena.java
 *
 * Created on 22 de septiembre de 2008, 21:35
 */

package gui;

import controladores.ControladorUsuario;
import javax.swing.JOptionPane;

/**
 *
 * @author  demian
 */
public class PanelContrasena extends javax.swing.JPanel {
    private ControladorUsuario controlador = new ControladorUsuario();
    private boolean habilitarUsuario = false;
    
    /** Creates new form PanelContrasena */
    public PanelContrasena() {
        initComponents();
    }
    
    /** This method is called from within the constructor tos
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPasswordField = new javax.swing.JPasswordField();
        jButtonAceptar = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();

        jLabel1.setText("Ingrese Contraseña");

        jPasswordField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordFieldActionPerformed(evt);
            }
        });

        jButtonAceptar.setText("Aceptar");
        jButtonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAceptarActionPerformed(evt);
            }
        });

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonCancelar))
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonAceptar)
                    .addComponent(jButtonCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAceptarActionPerformed
        int length = jPasswordField.getPassword().length;
        
        if(length == 0) {
            JOptionPane.showMessageDialog(this, "Debe ingresar una contraseña.");
            jPasswordField.requestFocusInWindow();
        }
        
       char password[] =  jPasswordField.getPassword();
       
       try {
           if(!controlador.validarContrasenaUsuario("administrador", password)) {
                JOptionPane.showMessageDialog(this, "Contraseña invalida.");
                jPasswordField.setText("");
                jPasswordField.requestFocusInWindow(); 
                habilitarUsuario = false;
           }  else {
                jPasswordField.setText("");
               habilitarUsuario = true;
               setVisible(false);
           }
       } catch(Exception e) {
           String msg = e.getMessage();
           JOptionPane.showMessageDialog(this, "Validacion de usuarios no disponible. " + (msg == null ? "" : "\nRazon: " + msg));
          jPasswordField.setText("");
          jPasswordField.requestFocusInWindow();
          habilitarUsuario = false;           
       }
    }//GEN-LAST:event_jButtonAceptarActionPerformed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        jPasswordField.setText("");
        habilitarUsuario = false;
        setVisible(false);
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void jPasswordFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordFieldActionPerformed
        this.jButtonAceptarActionPerformed(null);
    }//GEN-LAST:event_jPasswordFieldActionPerformed
    
    public boolean habilitarUsuario() {
        return this.habilitarUsuario;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAceptar;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPasswordField jPasswordField;
    // End of variables declaration//GEN-END:variables
    
}
